$(document).ready(function () {
   
      /*
       * Button 'Tambahkan' onClick event handler
       * this handler handling schedule temp saving process
       */
      $('#worktime_add').click(function () {
         var jadwal_kerja_cycle = $('#JadwalKerjaPerputaran :selected').val();
         var jadwal_kerja_name = $('#JadwalKerjaJamKerja :selected').html();
         var jam_kerja = $('#JadwalKerjaJamKerja :selected').val();
         var jadwal_kerja_hari = [];

         var jadwal_info = {
            'jadwal_kerja_cycle':jadwal_kerja_cycle,
            'jam_kerja':jam_kerja
         };


         // Init dates order
         // index start from zero because ajax data transfer automaticaly assign zero and undefined value to the first index of the array
         // this is what i mean --> data: {'days':jadwal_kerja_hari, 'info':jadwal_info}
         // it will be [days] => Array ([0] => undefined , [1] => Array ( [hari] => 0 ))

         for (var x=0; x<parseInt(jadwal_kerja_cycle); x++) {
            jadwal_kerja_hari[x] = {'hari':0, 'jam_kerja_id':0, 'jadwal_name':'empty'};
         }
         // end init dates order

         // Populate checkboxes
         for (var i=0;i<parseInt(jadwal_kerja_cycle); i++) {
            var chkbox = $('#JadwalKerjaHari'+i);

            if ((chkbox.attr('checked')) && (chkbox.attr('disabled') != true)) {
               jadwal_kerja_hari[i].hari = i+1;
               jadwal_kerja_hari[i].jam_kerja_id = jam_kerja;
               jadwal_kerja_hari[i].jadwal_name = jadwal_kerja_name;
            }

         }
         
         $.ajax({
            type: 'get',
            url: app_root_path+'absensi_ajax/jadwalKerjaCreateSession',
            data: {'days':jadwal_kerja_hari, 'info':jadwal_info},
            dataType: 'json',
            cache: 'false',
            success: function (data) {

               var tableHTML = [];

               for (var z=0; z<parseInt(jadwal_kerja_cycle); z++) {
                  if (data.jadwal_hari[z+1].hari != 0) {
                     var chkbox = $('#JadwalKerjaHari'+z);
                     chkbox.attr('checked', 'checked');
                     chkbox.attr('disabled','disabled');

                     tableHTML.push('<tr>');
                     tableHTML.push('<td>Hari '+data.jadwal_hari[z+1].hari+'</td>');
                     tableHTML.push('<td>'+data.jadwal_hari[z+1].jadwal_name+'</td>');
                     tableHTML.push('</tr>');
                  }
               }

               $('#jadwal_item').html(tableHTML.join('')).fadeOut().fadeIn();
            }
         }
         );


         }
      );
      
      /*
       * Drop Down Choice Perputaran 
       * onChange event handler
       */
      $('#JadwalKerjaPerputaran').change(
         function () {
            var days_container = $('.inputs_wrap');
            var days = [];
            var selected = $('#JadwalKerjaPerputaran :selected');

            $.ajax({
            type: 'get',
            url: app_root_path+'absensi_ajax/jadwalKerjaDeleteSession',
            dataType: 'json',
            cache: 'false'
            }
            );

            days.push('<input type="hidden" id="JadwalKerjaHari" value="" name="data[JadwalKerja][hari]" autocomplete="off">');

            for (var i=0; i<parseInt(selected.val()); i++) {
               days.push('<div class="checkbox">');
               days.push('<input type="checkbox" id="JadwalKerjaHari'+i+'" value="'+i+'" name="data[JadwalKerja][hari][]" autocomplete="off">');
               days.push('<label for="JadwalKerjaHari'+i+'">Hari '+(i+1)+'</label>');
               days.push('</div>');
            }

            days_container.html(days.join('')).fadeOut().fadeIn();
            $('#added_work_time').html('<table class="index"  width="100%" cellspacing="0" cellpadding="0"><tr><th>Hari</th><th>Jam Kerja</th></tr><tbody id="jadwal_item"></tbody></table>');
         }
      );
}
);